#!/bin/bash
#d1s patch
# ref openwrt-21.02.7
TINA_PATH=~/d1s/Tina-Linux
PATCH_PATH=$PWD
MKBOOTIMG_SRC_PATH=${PATCH_PATH}/mkbootimg
PYTHON2_SCRIPT_FILE=${PATCH_PATH}/scrips/src/pyhon2/buildpython2.sh
# install python3-setuptools
do_install () {
echo -e "\033[1;33m install python3 python3-setuptools ... \033[0m"
cp -p -f -v  ${PYTHON2_SCRIPT_FILE} ${PATCH_PATH}/Tina-Linux
sudo apt install python3 python3-setuptools
echo -e "\033[1;33mdone...\033[0m"
}

#mkbootimg
do_make_mkbootimg() {
echo -e "\033[1;33m install making mkbootimg ... \033[0m"
cd ${MKBOOTIMG_SRC_PATH}
make clean
make
echo -e "\033[1;33mdone...\033[0m"
}

#copy mkbootimg and make clean
do_copy_mkbootimg() {
echo -e "\033[1;33m copy mkbootimg ... \033[0m"
cd ${MKBOOTIMG_SRC_PATH}
chmod +x ${MKBOOTIMG_SRC_PATH}/mkbootimg
cp -p -f -v ${MKBOOTIMG_SRC_PATH}/mkbootimg ${PATCH_PATH}/Tina-Linux/tools/pack-bintools/src
cd ${MKBOOTIMG_SRC_PATH}
make clean
echo -e "\033[1;33m done...\033[0m"
}

# del tina files
do_del_files() {
echo -e "\033[1;33m start do_del_files ... \033[0m"
if [ ! -d "${TINA_PATH}" ];then 
echo -e "\033[1;31m not find Tina-Linux ath ... \033[0m"
fi
cd ${TINA_PATH}
if [ -d "${TINA_PATH}/.git" ];then
echo rename .git
mv -f -v ${TINA_PATH}/.git ${TINA_PATH}/.git.bak
fi 
rm -rf ${TINA_PATH}/tools/m4
rm -rf ${TINA_PATH}/tools/fakeroot
rm -rf ${TINA_PATH}/tools/mklibs
# rm -rf ${TINA_PATH}/tools/scons
rm -rf ${TINA_PATH}/tools/gengetopt
rm -rf ${TINA_PATH}/tools/squashfskit4
rm -rf ${TINA_PATH}/tools/cmake
echo -e "\033[1;33mdone...\033[0m"
}

# copy patch file to tina
do_copy_patch(){

echo -e "\033[1;33m start do_copy_patch ... \033[0m" 
#chmod +x ${PATCH_PATH}/Tina-Linux/tools/scons/files/pywrap.sh

#makefile version.date
date +%s > ${TINA_PATH}/version.date

cp -f -r -p -d   ${PATCH_PATH}/Tina-Linux/* ${TINA_PATH}/
echo -e "\033[1;33m all done...\033[0m"

}

set -e
do_install 

#mkbootimg make and copy
do_make_mkbootimg
do_copy_mkbootimg 

#file copy
do_del_files
do_copy_patch

